package cn.gl.product.service.impl;

import cn.gl.basic.service.impl.BaiduAiAuditServiceImpl;
import cn.gl.basic.util.AjaxResult;
import cn.gl.product.domain.Product;
import cn.gl.product.domain.ProductDetail;
import cn.gl.product.domain.ProductOnlineAuditLog;
import cn.gl.product.domain.Product;
import cn.gl.product.domain.ProductOnlineAuditLog;
import cn.gl.product.mapper.ProductDetailMapper;
import cn.gl.product.mapper.ProductMapper;
import cn.gl.product.mapper.ProductOnlineAuditLogMapper;
import cn.gl.product.service.IProductService;
import cn.gl.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private BaiduAiAuditServiceImpl baiduAiAuditService;
    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;
    
    @Override
    public void insert(Product product) {
        //添加宠物信息
        super.insert(product);
        //添加宠物详情
        ProductDetail productDetail = product.getProductDetail();
        if(productDetail!=null){
            productDetail.setProductId(product.getId());
            productDetailMapper.insert(productDetail);
        }
    }

    @Override
    public void delete(Long id) {
        //删除宠物详情
        productDetailMapper.deleteByProductId(id);
        //删除宠物信息
        super.delete(id);
    }

    @Override
    public void update(Product product) {
        super.update(product);
        ProductDetail productDetail = product.getProductDetail();
        if(productDetail!=null){
            productDetail.setProductId(product.getId());
            productDetailMapper.updateByProductId(productDetail);
        }
    }

    @Override
    public void patchDelete(List<Long> ids) {
        for (Long id : ids) {
            delete(id);
        }
    }

    /**
     * 服务上架
     * @param ids
     * @return
     */
    @Override
    public AjaxResult onsale(List<Long> ids) {
        for (Long id : ids) {
            //查询出单个商品id
            Product product = productMapper.laodOne(id);
            //百度ai文字审核
            Boolean boo1 = baiduAiAuditService.textAudit(product.getName());
            //百度ai图片审核
            Boolean boo2 = baiduAiAuditService.imageAudit(product.getResources());
            if(boo1 && boo2){//审核通过
                //改product状态:state = 1,上架时间
                productMapper.updateOnsaleState(id,new Date());
                //添加审核日志
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();//创建日志对象
                //添加id
                log.setProductId(id);
                //添加状态
                log.setState(1);
                //添加备注
                log.setNote(id+"审核成功");
                //添加数据库
                productOnlineAuditLogMapper.insert(log);
            }else{
                //添加审核日志
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();//创建日志对象
                //添加id
                log.setProductId(id);
                //添加状态
                log.setState(0);
                //添加备注
                log.setNote(id+"审核失败");
                //添加数据库
                productOnlineAuditLogMapper.insert(log);
                //返回失败信息
                return AjaxResult.me().setSuccess(false).setMsg("上架失败!!!");
            }
        }
        //返回前端展示
        return new AjaxResult();
    }

    /**
     * 服务下架
     * @param ids
     */
    @Override
    public void offsale(List<Long> ids) {
        //修改下架状态
        productMapper.updateOffState(ids,new Date());
    }
}
